package net.xdclass.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.CouponCategoryEnum;
import net.xdclass.request.NewUserCouponRequest;
import net.xdclass.service.CouponService;
import net.xdclass.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 孙浩
 * @since 2022-04-20
 */
@Api(tags = "优惠券模块")
@RestController
@RequestMapping("/api/coupon/v1")
public class CouponController {

  @Autowired
  private CouponService couponService;

  /**
   * 分页查询优惠券列表
   *
   * @return
   */
  @ApiOperation(value = "分页查询优惠券列表")
  @GetMapping("/page_coupon")
  public JsonData getCouponList(
      @ApiParam(value = "页数", defaultValue = "1")
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @ApiParam(value = "条数", defaultValue = "10")
      @RequestParam(value = "size", defaultValue = "10") Integer size) {
    return JsonData.buildSuccess(couponService.pageCouponActivity(page, size));
  }

  /**
   * 用户领取优惠券
   */
  @ApiOperation(value = "用户领取优惠券")
  @GetMapping("/add/promotion/{coupon_id}")
  public JsonData addPromotionCoupon(@ApiParam(value = "优惠券Id") @PathVariable(value = "coupon_id") String couponId) {
    couponService.addCoupon(couponId, CouponCategoryEnum.PROMOTION.name());
    return JsonData.buildSuccessMessage("领取成功");
  }

  /**
   * 新用户注册发放优惠券
   */
  @ApiOperation(value = "新用户注册发放优惠券")
  @PostMapping("/new_user_coupon")
  public JsonData newUserCoupon(@ApiParam(value = "用户领取对象") @RequestBody NewUserCouponRequest newUserCouponRequest) {
    couponService.initNewUserCoupon(newUserCouponRequest);
    return JsonData.buildSuccess();
  }
}

